from inputAndOutput import predict_mental_health
from flask import jsonify
import torch
import json

def predict123(data):
    # print(f"预测中的data:\n{data}\n")
    # try:
        json_data = json.loads(data)
        # Extract the numeric values from the JSON data
        numeric_values = [float(value) if '.' in value else int(value) for value in json_data.values() if value and (value.replace('.', '', 1).isdigit() or (value[0] == '-' and value[1:].isdigit()))]
        # 删掉mh_disorder_current列，因为这个应该是预测值



        # 删除numeric_values列表的后6个元素
        numeric_values = numeric_values[:-6]





        del numeric_values[-8]
        # 这里因为teach_flag数据还没有传过来，先给它一个假的值
        numeric_values[-2]=1
        # Convert the numeric values to PyTorch tensor
        tensor_data = torch.tensor([numeric_values], dtype=torch.float32)
        # 将 tensor_data 重新形状为所需形式（一维张量）
        tensor_data = tensor_data.view(-1)
        # 暂时输入用的excel表格的第三行数据
        # print(X_test_tensor[2])
        # print(len(X_test_tensor[2]))
        # print(tensor_data)
        # print(len(tensor_data))
        sample_data = torch.tensor(tensor_data, dtype=torch.float32).unsqueeze(0)  # 添加 batch 维度
        # 进行预测
        result = predict_mental_health(sample_data)
        if(result):
            print("yes")
        else:
            print("no")

        # Replace this with your actual model prediction logic
        # Make sure to adapt this to how you've loaded or trained your model
        # predict_with_your_model(text_data)
        response = {
            'result': result
        }
        return jsonify(response)
    # except Exception as e:
    #     print("抛出异常")
    #     print(str(e))  # 添加调试语句
    #     return jsonify({'error': str(e)})

# if __name__ == "__main__":
#     data = {"self_empl_flag": "1", "comp_no_empl": "0", "tech_comp_flag": "0", "mh_coverage_flag": "0", "mh_coverage_awareness_flag": "0", "mh_employer_discussion": "0", "mh_resources_provided": "0", "mh_anonimity_flag": "0", "mh_medical_leave": "0", "mh_discussion_neg_impact": "0", "ph_discussion_neg_impact": "0", "mh_discussion_cowork": "0", "mh_discussion_supervis": "0", "mh_eq_ph_employer": "0", "mh_conseq_coworkers": "0", "prev_employers_flag": "0", "prev_mh_benefits": "0", "prev_mh_benefits_awareness": "0", "prev_mh_discussion": "0", "prev_mh_resources": "0", "prev_mh_anonimity": "0", "prev_mh_discuss_neg_conseq": "0", "prev_ph_discuss_neg_conseq": "0", "prev_mh_discussion_cowork": "0", "prev_mh_discussion_supervisor": "0", "prev_mh_importance_employer": "0", "prev_mh_conseq_coworkers": "0", "future_ph_specification": "0", "why/ why_not": "0", "future_mh_specification": "0", "why/ why_not2": "0", "mh_hurt_on_career": "0", "mh_neg_view_cowork": "0", "mh_sharing_friends/fam_flag": "0", "mh_bad_response_workplace": "0", "mh_family_hist": "0", "mh_disorder_past": "0", "mh_disorder_current": "0", "mh_diagnos_proffesional": "0", "mh_sought_proffes_treatm": "0", "mh_eff_treat_impact_on_work": "0", "mh_not_eff_treat_impact_on_work": "0", "sex": "0", "province_live": "0", "city_live": "0", "province_work": "0", "city_work": "0", "work_position": "0", "remote_flag": "0"}
#     print(len(data))
#     predict1 = predict123(data)
#     print("预测代码的结果: ", predict1)